package com.grid.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;

import com.grid.db.ConnectMysql;
import com.grid.model.*;

@Path("/todo")
public class ApplicationResource {
  // This method is called if XMLis request
	

	private ArrayList<Application> ApplicationNames = new ArrayList<Application>();
	
	public ArrayList<Application> getApplicationNames() {
		return ApplicationNames;
	}
	
	public void setApplicationNames(ArrayList<Application> applicationNames) {
		ApplicationNames = applicationNames;
	}

	

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public ArrayList<Application> getXML() {
	  java.sql.ResultSet rs;
	  
		String query1 = "select applicationname from applicationmaster;";
		System.out.println(query1);
		rs = ConnectMysql.executeQuery(query1);
		try
		{
			while(rs.next())
			{
				Application tempapp = new Application();
				System.out.println(rs.getString(1));
				tempapp.setApplicationName(rs.getString(1));
				ApplicationNames.add(tempapp);
			}
		}
		catch(SQLException ex)
		{
			System.out.println("SQL exception occured");
		}
		
		return ApplicationNames;
  }
  
  
  
  // This can be used to test the integration with the browser
  @GET
  @Produces({ MediaType.TEXT_XML })
  public ArrayList<Application> getHTML() {
	  java.sql.ResultSet rs;
	  Application tempapp = new Application();
		String query1 = "select applicationname from applicationmaster;";
		rs = ConnectMysql.executeQuery(query1);
		try
		{
			while(rs.next())
			{
				System.out.println(rs.getString(1));
				tempapp.setApplicationName(rs.getString(1));
				ApplicationNames.add(tempapp);
			}
		}
		catch(SQLException ex)
		{
			System.out.println("SQL exception occured");
		}
		
		return ApplicationNames;
  }
  
} 